package net.zjitc.queue;

public class ArrayQueue<T> implements MyQueue<T> {
    private int MaxSize = 10;
    private Object[] arr= new Object[MaxSize];
    private int front = 0;
    private int rear = 0;

    public ArrayQueue(int maxSize) {
        MaxSize = maxSize;
        arr = new Object[maxSize];
    }

    public ArrayQueue() {
    }

    /**
     * 判断是否为空
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return rear == front?true:false;
    }

    /**
     * 入队
     *
     * @param x
     * @return
     */
    @Override
    public boolean add(T x) {
        if (isFull()){
            throw  new RuntimeException("队列已经满");
        }
        arr[rear]=x;
        rear = (rear+1)%MaxSize;
        return true;
    }

    /**
     * 出队
     *
     * @return
     */
    @Override
    public T poll() {
        if (isEmpty()){
            return null;
        }
        T var = (T) arr[front];
        arr[front] = null;
        front = (front+1)%MaxSize;
        return var;
    }

    /**
     * 查看队头元素，但不出队
     *
     * @return
     */
    @Override
    public T peek() {
        return (T) arr[front];
    }

    public boolean isFull(){
        return (rear+1)%MaxSize == front?true:false;
    }

    /**
     *  打印出所有有效的元素
     */

    public void print(){

    }
}
